import {
    getTopMv
} from '../../service/api_video'
Page({

    data: {
        topMvs: [],
        hasMore: true
    },
    onLoad: function (options) {
        this.getTopMvData(0)
    },
    // 函数封装
    async getTopMvData(offset) {
        if (!this.data.hasMore && offset === 0) return
        // wx.showNavigationBarLoading()
        const res = await getTopMv(offset)
        let newData = this.data.topMvs
        if (offset === 0) {
            newData = res.data
        } else {
            newData = newData.concat(res.data)
        }
        this.setData({
            topMvs: newData
        })
        this.setData({
            hasMore: res.hasMore
        })
        if (offset === 0) {
            wx.stopPullDownRefresh()
        }
    },
    // 点击item
    handleVideoItemClick(e){
        const id = e.target.dataset.id
        wx.navigateTo({
          url: `/pages/detail-video/index?id=${id}`,
        })
    },
    // 当达到底部会回调该函数
    onReachBottom: async function () {
        this.getTopMvData(this.data.topMvs.length)
    },
    //下拉刷新
    onPullDownRefresh: function () {
        this.getTopMvData(0)
    }
})